<template>
    <div class="navbar">
        <span>九月Stu</span>
        <div class="user-info">
            当前用户：{{ username }}
            <button @click="logout" class="logout-btn">退出登录</button>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            username: ''
        };
    },
    created() {
        const userInfo = JSON.parse(localStorage.getItem('userInfo'));
        if (userInfo) {
            this.username = userInfo.username;
        }
    },
    methods: {
        logout() {
            // 清除 localStorage 中的用户信息
            localStorage.removeItem('userInfo');

            // 模拟重定向到登录页面或首页
            window.location.href = '/login';
        }
    }
};
</script>

<style scoped>
.navbar {
    margin-left: 40px;
    width: 86%;
    height: 60px;
    background: linear-gradient(135deg, #42b983, #358d73);
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    position: fixed;
    top: 0;
    left: 200px;
    box-sizing: border-box;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    z-index: 1000;
}

.navbar span {
    color: white;
    font-size: 20px;
    font-weight: bold;
}

.user-info {
    color: white;
    font-size: 16px;
}

.logout-btn {
    margin-left: 20px;
    background-color: #f56c6c;
    color: white;
    border: none;
    padding: 5px 10px;
    cursor: pointer;
    font-size: 14px;
    border-radius: 4px;
    transition: background-color 0.3s;
}

.logout-btn:hover {
    background-color: #ff4949;
}
</style>